﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Booking : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Session["noResult"] = null;
        if (Session["Pasagers"] == null || Session["ChosenPath"] == null || Session["Login"] == null)
        {
            Server.Transfer("~/Default.aspx");
        }
        else
        {

            ServiceReference.ServiceClient service = new ServiceReference.ServiceClient();
            ServiceReference.Airport fromAirport = new ServiceReference.Airport();
            ServiceReference.Airport toAirport = new ServiceReference.Airport();

            List<ServiceReference.BookingLine> listOfBookingLines = new List<ServiceReference.BookingLine>();
            List<int> schedulesId = new List<int>();

            ServiceReference.BookingLine bookingLine = new ServiceReference.BookingLine();

            //table header
            
           

            TableRow tr = new TableRow();
            TableCell td = new TableCell();
            td.CssClass = "rowStyle";

            Label lb = new Label();
            lb.Text = "Departure";
            lb.Font.Size = FontUnit.Larger;
            lb.Font.Bold = true;

            td = new TableCell();
            td.CssClass = "rowStyle";
            td.Controls.Add(lb);
            tr.Cells.Add(td);

            lb = new Label();
            lb.Text = "From";
            lb.Font.Size = FontUnit.Larger;
            lb.Font.Bold = true;
            td = new TableCell();
            td.CssClass = "rowStyle";
            td.Controls.Add(lb);
            tr.Cells.Add(td);



            lb = new Label();
            lb.Text = "Arival";
            lb.Font.Size = FontUnit.Larger;
            lb.Font.Bold = true;
            td = new TableCell();
            td.CssClass = "rowStyle";
            td.Controls.Add(lb);
            tr.Cells.Add(td);

            lb = new Label();
            lb.Text = "To";
            lb.Font.Size = FontUnit.Larger;
            lb.Font.Bold = true;
            td = new TableCell();
            td.CssClass = "rowStyle";
            td.Controls.Add(lb);
            tr.Cells.Add(td);



            lb = new Label();
            lb.Text = "Price";
            lb.Font.Size = FontUnit.Larger;
            lb.Font.Bold = true;
            td = new TableCell();
            td.CssClass = "rowStyle";
            td.Controls.Add(lb);
            tr.Cells.Add(td);


            lb = new Label();
            lb.Text = "Free Seats";
            lb.Font.Size = FontUnit.Larger;
            lb.Font.Bold = true;
            td = new TableCell();
            td.CssClass = "rowStyle";
            td.Controls.Add(lb);
            tr.Cells.Add(td);

            results.Rows.Add(tr);

            foreach (ServiceReference.EdgeOfAirportScheduleL4OapnGX edge in ((ServiceReference.PathOfAirportScheduleL4OapnGX)Session["BookingLine" + Session["ChosenPath"]]).Edges)
            {

                fromAirport = edge.From.Object;
                toAirport = edge.To.Object;


                tr = new TableRow();
                td = new TableCell();
                td.CssClass = "rowStyle";


                td = new TableCell();
                td.CssClass = "rowStyle";
                td.Text = edge.Data.startTime.ToString();
                tr.Cells.Add(td);

                td = new TableCell();
                td.CssClass = "rowStyle";
                td.Text = fromAirport.name;
                tr.Cells.Add(td);



                td = new TableCell();
                td.CssClass = "rowStyle";
                td.Text = edge.Data.endTime.ToString();
                tr.Cells.Add(td);

                td = new TableCell();
                td.CssClass = "rowStyle";
                td.Text = toAirport.name;
                tr.Cells.Add(td);



                td = new TableCell();
                td.CssClass = "rowStyle";
                td.Text = edge.Data.price.ToString();
                tr.Cells.Add(td);

                td = new TableCell();
                td.CssClass = "rowStyle";
                td.Text = service.getFreeSeats(edge.Data.scheduleId).ToString();
                tr.Cells.Add(td);

                results.Rows.Add(tr);

                bookingLine.price += edge.Data.price;

                //adding schedules id to the list
                schedulesId.Add(edge.Data.scheduleId);
            }


            for (int i = 0; i < Convert.ToInt32(Session["Pasagers"]); i++)
            {

                tr = new TableRow();
                td = new TableCell();
                TextBox txt = new TextBox();


                //First Name
                lb = new Label();
                lb.Text = "Person: " + (i + 1).ToString();
                lb.Font.Size = FontUnit.Larger;
                lb.Font.Bold = true;
                td.Controls.Add(lb);
                tr.Controls.Add(td);
                Persons.Controls.Add(tr);

                //td.Text = "Person: " + i + 1;

                //tr.Controls.Add(td);
                //Persons.Controls.Add(tr);

                lb = new Label();
                lb.Text = "First Name";


                td = new TableCell();
                tr = new TableRow();
                td.Controls.Add(lb);
                tr.Controls.Add(td);

                txt.ID = "fNameTxt" + i.ToString();
                td = new TableCell();
                td.Controls.Add(txt);
                tr.Controls.Add(td);
                Persons.Controls.Add(tr);

                //Surname

                lb = new Label();
                lb.Text = "Surname";

                td = new TableCell();
                tr = new TableRow();
                td.Controls.Add(lb);
                tr.Controls.Add(td);

                txt = new TextBox();
                txt.ID = "sNameTxt" + i.ToString();
                td = new TableCell();
                td.Controls.Add(txt);
                tr.Controls.Add(td);
                Persons.Controls.Add(tr);

                //Passport No

                lb = new Label();
                lb.Text = "Passport No";

                td = new TableCell();
                tr = new TableRow();
                td.Controls.Add(lb);
                tr.Controls.Add(td);

                txt = new TextBox();
                txt.ID = "passportTxt" + i.ToString();
                td = new TableCell();
                td.Controls.Add(txt);
                tr.Controls.Add(td);
                Persons.Controls.Add(tr);

                //Phone

                lb = new Label();
                lb.Text = "Phone";

                td = new TableCell();
                tr = new TableRow();
                td.Controls.Add(lb);
                tr.Controls.Add(td);

                txt = new TextBox();
                txt.ID = "phoneTxt" + i.ToString();
                td = new TableCell();
                td.Controls.Add(txt);
                tr.Controls.Add(td);
                Persons.Controls.Add(tr);

                //Address

                lb = new Label();
                lb.Text = "Address";

                td = new TableCell();
                tr = new TableRow();
                td.Controls.Add(lb);
                tr.Controls.Add(td);

                txt = new TextBox();
                txt.ID = "addressTxt" + i.ToString();
                td = new TableCell();
                td.Controls.Add(txt);
                tr.Controls.Add(td);
                Persons.Controls.Add(tr);

                //Gender

                lb = new Label();
                lb.Text = "Gender";

                td = new TableCell();
                tr = new TableRow();
                td.Controls.Add(lb);
                tr.Controls.Add(td);


                DropDownList genderList = new DropDownList();
                genderList.Items.Add("Male");
                genderList.Items.Add("Female");
                genderList.ID = "genderList" + i.ToString();

                td = new TableCell();
                td.Controls.Add(genderList);
                tr.Controls.Add(td);
                Persons.Controls.Add(tr);

            }

            listOfBookingLines.Add(bookingLine);

            Session["ListOfBookingLineIds"] = listOfBookingLines;
            Session["schedulesId"] = schedulesId;
            service.Close();
        }




    }

    protected void Page_Init(object sender, EventArgs e)
    {

    }

    public void FinishBtn_Click(object sender, EventArgs e)
    {
        ServiceReference.ServiceClient service = new ServiceReference.ServiceClient();

        List<ServiceReference.BookingLine> bookingLines = (List<ServiceReference.BookingLine>)Session["ListOfBookingLineIds"];
        List<int> schedules = (List<int>)Session["schedulesId"];
        List<ServiceReference.Person> persons = new List<ServiceReference.Person>();

        bool check = true;

        for (int i = 0; i < (int)Session["Pasagers"]; i++)
        {

            ServiceReference.Person pers = new ServiceReference.Person();

            TextBox TXTBOX;
            TXTBOX = results.FindControl("fNameTxt" + i.ToString()) as TextBox;
            if (TXTBOX.Text != "")
                pers.fName = TXTBOX.Text;
            else check = false;

            TXTBOX = results.FindControl("sNameTxt" + i.ToString()) as TextBox;
            if (TXTBOX.Text != "")
                pers.sName = TXTBOX.Text;
            else check = false;

            TXTBOX = results.FindControl("passportTxt" + i.ToString()) as TextBox;
            if (TXTBOX.Text != "")
                pers.passportNo = TXTBOX.Text;
            else check = false;

            TXTBOX = results.FindControl("phoneTxt" + i.ToString()) as TextBox;
            if (TXTBOX.Text != "")
                pers.phone = TXTBOX.Text;
            else check = false;

            TXTBOX = results.FindControl("addressTxt" + i.ToString()) as TextBox;
            if (TXTBOX.Text != "")
                pers.address = TXTBOX.Text;
            else check = false;

            DropDownList genderList;
            genderList = results.FindControl("genderList" + i.ToString()) as DropDownList;
            if (genderList.SelectedIndex == 0)
                pers.gender = true;
            else
                pers.gender = false;

            persons.Add(pers);
            // }
        }

        if (check == true)
        {

            if (service.makeBooking(Convert.ToInt32(((ServiceReference.Account)Session["Login"]).accountId), bookingLines.ToArray(), schedules.ToArray(), persons.ToArray()))
            {
                Session["bookingResult"] = true;

            }
            else
                Session["bookingResult"] = false;

            Server.Transfer("~/BookingResponse.aspx");

            service.Close();
        }
        else
        {
            checkLabel.Text = "Please check that all the information are filled in!";
        }
    }
}